var admin_post_apis = {
	create_post: "http://api.fangchepingtai.com/fangchebbs/admin/post/create",
	update_post: "http://api.fangchepingtai.com/fangchebbs/admin/post/update",
	detail_post: "http://api.fangchepingtai.com/fangchebbs/admin/post/detail/",
	module_list: "http://api.fangchepingtai.com/fangchebbs/admin/module/listall"
};
(function() {
	var editor, post_id;

	// 选择版块
	App.set("$Event.admin.post.select_module", function() {
		var module = App.get("$Cache.admin.post.module_list")[this.value];
		if (module) {
			App.set("$Cache.admin.post.new._module", module._id);
			App.set("$Cache.admin.post.new._module_name", module.name);
		};
	});

	// 获取ueditor内容
	App.set("$Evemt.admin.post.submit_post", function() {
		var detail = editor.getContent();
		if (!detail) {
			alert("warn", "请输入话题内容");
			return;
		};
		if (!App.get("$Cache.admin.post.new.title")) {
			alert("warn", "请输入话题标题");
			return;
		};
		if (!App.get("$Cache.admin.post.new._module")) {
			alert("warn", "请选择话题版块");
			return;
		};
		var data = App.get("$Cache.admin.post.new");
		data.detail = detail;
		var method = post_id ? "put" : "post";
		var post_url = post_id ? admin_post_apis.update_post : admin_post_apis.create_post;
		coAjax[method](post_url, data, function(result) {
			console.log(result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
		// console.log(editor.getContent());
	});

	// 获取话题详情
	function getPostDetail(_id) {
		App.set("$Cache.admin.post.detail_loading", true);
		coAjax.get(admin_post_apis.detail_post + _id, function(result) {
			console.log(result);
			App.set("$Cache.admin.post.new", result);
			var _editor_timer = setInterval(function() {
				if (editor) {
					editor.addListener("ready", function() {
						// editor准备好之后才可以使用
						editor.setContent(result.detail);
						App.set("$Cache.admin.post.detail_loading", false);
					});
					clearInterval(_editor_timer);
				};
			});
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	};

	Path.on("/admin/__basename__.html", function(_current_location) {
		var qs = _current_location.query;
		post_id = qs.get("_id");
		post_id ? getPostDetail(post_id) : false;
		// 获取版块列表
		coAjax.get(admin_post_apis.module_list, function(result) {
			// console.log(result);
			App.set("$Cache.admin.post.module_list", result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
		// 初始化编辑器
		require(["ueditor"], function() {
			editor = UE.getEditor('edit');
		});
	});

	require(['/js/tools/image_upload.js'], function(upload) {
		// 上传话题图片
		App.set("$Event.admin.post.upload_post_preview", function() {
			App.set("$Cache.admin.post.uploading", true);
			var file = this.files[0];
			// var dtd = new $.Deferred();
			if (file) {
				upload(file, function(path) {
					App.set("$Cache.admin.post.new.preview_img", path);
					App.set("$Cache.admin.post.uploading", false);
				});
			} else {
				App.set("$Cache.admin.post.uploading", false);
			};
		});
	});

	// 删除封面
	App.set("$Event.admin.post.delete_post_preview", function() {
		App.set("$Cache.admin.post.new.preview_img", "");
	});
}());